package com.cpg.school.controller.service;

import com.cpg.school.mybatis.mapper.PrincipalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 校长接口
 */
@RestController
public class PrincipalService {

    @Autowired
    private PrincipalMapper principalMapper;

    @PostMapping("/department_add.do")
    public String department_add(String name) {
        int have = principalMapper.department_have(name);
        if (have > 0) {
            return "名称已存在，请勿重复添加。";
        }
        int status = principalMapper.department_add(name);
        return status > 0 ? "新增成功！" : "新增失败！";
    }

    @PostMapping("/department_update.do")
    public String department_update(int id, String name) {
        int have = principalMapper.department_have(name);
        if (have > 0) {
            return "名称已存在，请勿重复添加。";
        }
        int status = principalMapper.department_update(id, name);
        return status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/department_del.do")
    public String department_del(int id) {
        int status = principalMapper.department_del(id);
        return status > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/grade_add.do")
    public String grade_add(int depar, String year, String name) {
        int status = principalMapper.grade_add(depar, year, name);
        return status > 0 ? "添加成功！" : "添加失败！";
    }

    @GetMapping("/grade_del.do")
    public String grade_del(int id) {
        int status = principalMapper.grade_del(id);
        return status > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/grade_update.do")
    public String grade_update(int id, String year, String name) {
        int status = principalMapper.grade_update(id, year, name);
        return status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/grade_teacher_del.do")
    public String grade_teacher_del(int id) {
        int status = principalMapper.grade_teacher_del(id);
        return status > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/grade_teacher_add.do")
    public String grade_teacher_add(int id, String teacher_type_id, String teacher_id) {
        int status = principalMapper.grade_teacher_add(id, teacher_type_id, teacher_id);
        return status > 0 ? "添加成功！" : "添加失败！";
    }

    @PostMapping("/teacher_type_add.do")
    public String teacher_type_add(String type_name, String type_explain) {
        int status = principalMapper.teacher_type_add(type_name, type_explain);
        return status > 0 ? "添加成功！" : "添加失败！";
    }

    @PostMapping("/teacher_type_edit.do")
    public String teacher_type_edit(int id, String type_name, String type_explain) {
        int status = principalMapper.teacher_type_edit_update(id, type_name, type_explain);
        return status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/teacher_type_del.do")
    public String teacher_type_del(int id) {
        int status = principalMapper.teacher_type_del(id);
        return status > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/teacher_add.do")
    public String teacher_add(String teacher_name, String login_name, String login_pass) {
        //判断用户登录名是否存在
        int have = principalMapper.login_name_ishave(login_name);
        if (have > 0) return "登录名已存在，请重设。";
        int add_user = principalMapper.add_user_teacher(teacher_name, login_name, login_pass);
        return add_user > 0 ? "添加成功！" : "添加失败！";
    }

    @PostMapping("/teacher_update.do")
    public String teacher_update(int id, String teacher_name, String login_name, String login_pass) {
        int status = principalMapper.teacher_update(id, teacher_name, login_name, login_pass);
        return status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/teacher_del.do")
    public String teacher_del(int id) {
        int status = principalMapper.teacher_del(id);
        return status > 0 ? "删除成功！" : "删除失败！";
    }
}
